﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Data;
using System.Data.SqlClient;

namespace XCT.Class
{
    class clsDangNhap
    {
        #region "Attributes"

        private string user;
        private string pass;
        private string tenUser;
        private string quyen;
        private int cLLH;
        private int bTP;
        private int eDC;
        private int dBG;

        #endregion

        #region "Properties"

        public string User
        {
            get { return user; }
            set { user = value; }
        }

        public string Pass
        {
            get { return MaHoa(pass); }
            set { pass = value; }
        }

        public string TenUser
        {
            get { return tenUser; }
            set { tenUser = value; }
        }

        public string Quyen
        {
            get { return quyen; }
            set { quyen = value; }
        }

        public int CLLH
        {
            get { return cLLH; }
            set { cLLH = value; }
        }

        public int BTP
        {
            get { return bTP; }
            set { bTP = value; }
        }

        public int EDC
        {
            get { return eDC; }
            set { eDC = value; }
        }

        public int DBG
        {
            get { return dBG; }
            set { dBG = value; }
        }

        #endregion

        #region "Hàm"

        public string MaHoa(string pMatKhau)
        {
            try
            {
                MD5 md5 = MD5.Create();
                StringBuilder strBuilder = new StringBuilder();
                Byte[] bMd5 = md5.ComputeHash(Encoding.Default.GetBytes(pMatKhau));
                for (int iloop = 0; iloop < bMd5.Length; iloop++)
                {
                    strBuilder.Append(bMd5[iloop].ToString("x2"));
                }
                return strBuilder.ToString();
            }
            catch (Exception ex)
            {                
                throw(ex);
            }
        }

        //Đọc dữ liệu người dùng
        public DataSet DocNguoiDung_XCT()
        {
            DataProvider dp = new DataProvider();

            return dp.fdtsDocDuLieuSP("sp_DocNguoiDung_XCT", null);
        }

        //Đọc dữ liệu người dùng theo MaNV
        public SqlDataReader DocNguoiDung_XCT_TheoMaNV()
        {
            DataProvider dp = new DataProvider();
            
            SqlParameter[] arrPara = new SqlParameter[1];
            arrPara[0] = new SqlParameter("@pMaNV", SqlDbType.VarChar);
            arrPara[0].Value = User;

            return dp.fdreadDocDuLieuSP("sp_DocNguoiDung_XCT_TheoMaNV", arrPara);
        }

        //Đổi mật khẩu người dùng
        public Boolean DoiMatKhau()
        {
            DataProvider dp = new DataProvider();
            SqlParameter[] arrPara = new SqlParameter[2];
            arrPara[0] = new SqlParameter("@pMaNV", SqlDbType.VarChar, 20);
            arrPara[0].Value = User;

            arrPara[1] = new SqlParameter("@pPass", SqlDbType.VarChar, 100);
            arrPara[1].Value = Pass;

            return dp.fBolThucThiSP("sp_SuaPassNguoiDung_XCT", arrPara);
        }

        //Thêm người dùng
        public Boolean ThemNguoiDung()
        {
            DataProvider dp = new DataProvider();
            SqlParameter[] arrPara = new SqlParameter[7];
            arrPara[0] = new SqlParameter("@pMaNV", SqlDbType.VarChar, 20);
            arrPara[0].Value = User;

            arrPara[1] = new SqlParameter("@pPass", SqlDbType.VarChar, 100);
            arrPara[1].Value = Pass;

            arrPara[2] = new SqlParameter("@pTenNV", SqlDbType.VarChar, 50);
            arrPara[2].Value = TenUser;

            arrPara[3] = new SqlParameter("@pCLLH", SqlDbType.Bit);
            arrPara[3].Value = CLLH;

            arrPara[4] = new SqlParameter("@pBTP", SqlDbType.Bit);
            arrPara[4].Value = BTP;

            arrPara[5] = new SqlParameter("@pEDC", SqlDbType.Bit);
            arrPara[5].Value = EDC;

            arrPara[6] = new SqlParameter("@pDBG", SqlDbType.Bit);
            arrPara[6].Value = DBG;

            return dp.fBolThucThiSP("sp_ThemNguoiDung_XCT", arrPara);
        }

        //Sửa người dùng
        public Boolean SuaNguoiDung()
        {
            DataProvider dp = new DataProvider();
            SqlParameter[] arrPara = new SqlParameter[3];
            arrPara[0] = new SqlParameter("@pMaNV", SqlDbType.VarChar, 20);
            arrPara[0].Value = User;

            arrPara[1] = new SqlParameter("@pPass", SqlDbType.VarChar, 100);
            arrPara[1].Value = Pass;

            arrPara[2] = new SqlParameter("@pTenNV", SqlDbType.VarChar, 50);
            arrPara[2].Value = TenUser;

            return dp.fBolThucThiSP("sp_SuaNguoiDung_XCT", arrPara);
        }
                
        //Xóa người dùng
        public Boolean XoaNguoiDung()
        {
            DataProvider dp = new DataProvider();

            SqlParameter[] arrPara = new SqlParameter[1];
            arrPara[0] = new SqlParameter("@pMaNV", SqlDbType.VarChar);
            arrPara[0].Value = User;

            return dp.fBolThucThiSP("sp_XoaNguoiDung_XCT", arrPara);
        }

        //Sửa phân quyền
        public Boolean SuaPhanQuyen()
        {
            DataProvider dp = new DataProvider();
            SqlParameter[] arrPara = new SqlParameter[5];
            arrPara[0] = new SqlParameter("@pMaNV", SqlDbType.VarChar, 20);
            arrPara[0].Value = User;

            arrPara[1] = new SqlParameter("@pCLLH", SqlDbType.Bit);
            arrPara[1].Value = CLLH;

            arrPara[2] = new SqlParameter("@pBTP", SqlDbType.Bit);
            arrPara[2].Value = BTP;

            arrPara[3] = new SqlParameter("@pEDC", SqlDbType.Bit);
            arrPara[3].Value = EDC;

            arrPara[4] = new SqlParameter("@pDBG", SqlDbType.Bit);
            arrPara[4].Value = DBG;

            return dp.fBolThucThiSP("sp_SuaPhanQuyen_XCT", arrPara);
        }

        #endregion "Hàm"
    }
}
